package org.example.springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.example.springboot.entity.Book;

import java.util.List;
import java.util.Map;

/**
 * 图书Mapper接口
 */
@Mapper
public interface BookMapper extends BaseMapper<Book> {
    
    /**
     * 获取图书分类分布数据
     * @return 每个分类的图书数量
     */
    @Select("SELECT bc.name AS categoryName, COUNT(b.id) AS bookCount " +
            "FROM book b " +
            "JOIN book_category bc ON b.category_id = bc.id " +
            "WHERE b.status = 1 " +
            "GROUP BY b.category_id " +
            "ORDER BY bookCount DESC")
    List<Map<String, Object>> selectCategoryDistribution();
    
    /**
     * 获取热门图书数据
     * @param limit 限制数量
     * @return 借阅次数最多的图书
     */
    @Select("SELECT title, borrowed_count AS borrowCount " +
            "FROM book " +
            "WHERE status = 1 " +
            "ORDER BY borrowed_count DESC " +
            "LIMIT #{limit}")
    List<Map<String, Object>> selectHotBooks(@Param("limit") int limit);

    /**
     * 查询评分最高的N本书
     * @param limit 限制数量
     * @return 评分最高的N本书
     */
    @Select("SELECT b.title, COALESCE(b.avg_score, 0) as rating " +
            "FROM book b " +
            "WHERE b.status = 1 " +
            "ORDER BY rating DESC " +
            "LIMIT #{limit}")
    List<Map<String, Object>> selectTopRatedBooks(@Param("limit") Integer limit);
} 